// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.editors.actions;

import java.util.List;

import org.dengues.core.metadata.MetadataHelper;
import org.dengues.core.process.CompProcessHelper;
import org.dengues.designer.ui.database.commands.DBColumnCreateCommand;
import org.dengues.designer.ui.database.commands.DBNoteCreateCommand;
import org.dengues.designer.ui.database.commands.DBTableCreateCommand;
import org.dengues.designer.ui.database.editors.GEFDatabaseEditor;
import org.dengues.designer.ui.editors.DatabaseMultiEditor;
import org.dengues.designer.ui.editors.ProcessMultiEditor;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.designer.ui.process.commands.CompBlockCreateCommand;
import org.dengues.designer.ui.process.commands.CompNodePasteCommand;
import org.dengues.designer.ui.process.commands.CompNoteCreateCommand;
import org.dengues.designer.ui.process.editors.GEFComponentsEditor;
import org.dengues.designer.ui.process.models.CompBlock;
import org.dengues.designer.ui.process.models.CompNode;
import org.dengues.designer.ui.process.models.CompNote;
import org.dengues.designer.ui.process.models.CompProcess;
import org.dengues.model.DenguesModelManager;
import org.dengues.model.database.DBColumn;
import org.dengues.model.database.DBNote;
import org.dengues.model.database.DBTable;
import org.dengues.model.database.DatabaseDiagram;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.ui.actions.Clipboard;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class GEFPasteAction extends SelectionAction {

    /**
     * Qiang.Zhang.Adolf@gmail.com GEFPasteAction constructor comment.
     * 
     * @param part
     */
    public GEFPasteAction(IWorkbenchPart part) {
        super(part);
        setId(ActionFactory.PASTE.getId());
        setText(Messages.getString("GEFPasteAction.paste")); //$NON-NLS-1$
        ISharedImages sharedImages = part.getSite().getWorkbenchWindow().getWorkbench().getSharedImages();
        setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_PASTE));
        setDisabledImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_PASTE_DISABLED));
    }

    @Override
    protected boolean calculateEnabled() {
        Object o = Clipboard.getDefault().getContents();
        if (!(o instanceof List)) {
            return false;
        }
        List objects = (List) Clipboard.getDefault().getContents();
        if (objects.isEmpty()) {
            return false;
        }
        boolean containDBColumn = true;
        for (Object object : objects) {
            containDBColumn = containDBColumn && (object instanceof DBColumn);
        }
        if (containDBColumn) {
            if (getSelectedObjects().size() != 1) {
                return false;
            }
            if (!(getSelectedObjects().get(0) instanceof EditPart)) {
                return false;
            }
            if (!(((EditPart) getSelectedObjects().get(0)).getModel() instanceof DBTable)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void run() {
        IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
        DatabaseDiagram diagram = null;
        CompProcess process = null;
        if (activeEditor instanceof DatabaseMultiEditor) {
            diagram = ((DatabaseMultiEditor) activeEditor).getGefDBEditor().getDatabaseDiagram();
        } else if (activeEditor instanceof ProcessMultiEditor) {
            process = ((ProcessMultiEditor) activeEditor).getGefCompEditor().getProcess();
        }
        List objects = (List) Clipboard.getDefault().getContents();
        if (!objects.isEmpty()) {
            Command command = null;
            for (Object object : objects) {
                if (process != null) {
                    command = pasteProcessElement(object, process);
                } else if (diagram != null) {
                    command = pasteDiagramElement(object, diagram);
                }
                if (command != null) {
                    execute(command);
                }
            }
        }

    }

    private Command pasteDiagramElement(Object object, DatabaseDiagram diagram) {
        Command command = null;
        if (object instanceof DBNote) {
            DBNote note = (DBNote) object;
            DBNote newNote = DenguesModelManager.getDatabaseFactory().createDBNote();
            Rectangle rectangle = new Rectangle(GEFDatabaseEditor.getMouseLocation(), new Dimension(note.getWidth(), note
                    .getHeight()));
            command = new DBNoteCreateCommand(diagram, rectangle, newNote);
        } else if (object instanceof DBTable) {
            DBTable table = (DBTable) object;
            DBTable newTable = MetadataHelper.cloneDBTable(table);
            Rectangle rectangle = new Rectangle(GEFDatabaseEditor.getMouseLocation(), new Dimension(table.getWidth(), table
                    .getHeight()));
            command = new DBTableCreateCommand(diagram, rectangle, newTable);
        } else if (object instanceof DBColumn) {
            Object model = ((EditPart) getSelectedObjects().get(0)).getModel();
            if (model instanceof DBTable) {
                DBTable table = (DBTable) model;
                DBColumn column = (DBColumn) object;
                command = new DBColumnCreateCommand(table, MetadataHelper.cloneDBColumn(column));
            }
        }
        return command;
    }

    private Command pasteProcessElement(Object object, CompProcess process) {
        Command command = null;
        if (object instanceof CompNode) {
            CompNode node = (CompNode) object;
            command = new CompNodePasteCommand(process, node, GEFComponentsEditor.getMouseLocation());
        } else if (object instanceof CompNote) {
            CompNote note = ((CompNote) object).clone();
            Rectangle rectangle = new Rectangle(GEFComponentsEditor.getMouseLocation(), note.getSize().getCopy());
            Point l = CompProcessHelper.generateElementNewLocation(note.getCompProcess(), note, rectangle);
            command = new CompNoteCreateCommand(process, note, new Rectangle(l.getCopy(), rectangle.getSize().getCopy()));
        } else if (object instanceof CompBlock) {
            CompBlock block = (CompBlock) ((CompBlock) object).clone();
            Rectangle rectangle = new Rectangle(GEFComponentsEditor.getMouseLocation(), block.getSize().getCopy());
            Point l = CompProcessHelper.generateElementNewLocation(block.getCompProcess(), block, rectangle);
            command = new CompBlockCreateCommand(process, block, new Rectangle(l.getCopy(), rectangle.getSize().getCopy()));
        }
        return command;
    }
}
